<template>
  <div>
    <Breadcrumb></Breadcrumb>
    <AddPatientDrawer></AddPatientDrawer>
    <UpdatePatientDrawer ref="updatePatient"></UpdatePatientDrawer>
    <a-table :columns="columns" :data-source="patientData" :pagination="{pageSize:7}">
      <a slot="name" slot-scope="text">{{ text }}</a>
      <span slot="customTitle">
        <a-icon type="smile-o"/>
        姓名
      </span>
      <span slot="gender" slot-scope="gender">
        <a-tag
            :color="gender === 1 ? 'geekblue' : 'red'"
        >
          {{ gender === 1 ? '男' : '女' }}
        </a-tag>
      </span>

      <span slot="action" slot-scope="text, record">
        <a @click="sendOnePatient(record.key)">Update</a>
        <a-divider type="vertical"/>
        <a @click="deleteOnePatient(record.key)">Delete</a>
        <a-divider type="vertical"/>
        <a class="ant-dropdown-link"> 查看往期病历 <a-icon type="down"/> </a>
      </span>
    </a-table>
  </div>
</template>

<script>
import axios from "axios";
import columns from './AdminPatient.js'
import AddPatientDrawer from "@/components/drawer/AddPatientDrawer.vue"
import UpdatePatientDrawer from "@/components/drawer/UpdatePatientDrawer.vue";
import {mapGetters} from "vuex";
import Breadcrumb from "@/components/Breadcrumb";
export default {
  name: "AdminPatient",
  components: {AddPatientDrawer, UpdatePatientDrawer,Breadcrumb},
  data() {
    return {
      patientData: [],
      columns,
    }
  },
  computed: {
    ...mapGetters(['getSign'])
  },
  methods: {
    getAdminPatient() {
      axios.get("/api/adminPatient/show")
          .then(response => {
            this.patientData = response.data
            console.log("data : ", response.data);
          })
          .catch(error => {
            console.log(error);
          });
    },
    //TODO:完成！
    deleteOnePatient(key) {
      axios({
        url: '/api/adminPatient/deleteOne/' + key,
        method: 'post'
      }).catch(res => {
        console.log('删除patient出错')
        console.log(res)
      })
      this.$nextTick(() => {
        this.getAdminPatient();
      })
    },
    //更新病人信息,发送数据
    sendOnePatient(key) {
      this.$refs.updatePatient.showUpdate();
      axios({
        url: `/api/adminPatient/getOnePatient/${key}`,
        method: 'get'
      }).then((res) => {
        //获取数据
        console.log('res.data = ', res.data)
        this.$bus.$emit('sendPatientData', res.data)
        this.$bus.$emit('showPatientUpdate')
      }).catch(res => {
        console.log("取出病人数据出错", res)
      })
    }
  },
  watch: {
    //监听sign值的变化
    getSign() {
      this.$nextTick(() => {
        this.getAdminPatient()
      })
    }

  },
  //提供病人基本数据
  mounted() {
    // let that = this;
    this.getAdminPatient();
    // console.log('token' + this.$session.get('token'));
    if(this.$session.get('token')==null){
      this.$router.push("/login")
    }
  }
}
</script>

<style scoped>

</style>